package com.sanjin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sanjin.domain.LoginUser;
import com.sanjin.domain.UmsSysUser;
import com.sanjin.mapper.UmsSysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UmsSysUserMapper userMapper;


    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        LambdaQueryWrapper<UmsSysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsSysUser::getUsername,userName);
        UmsSysUser umsSysUser = userMapper.selectOne(wrapper);
        if(umsSysUser == null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        //查询权限信息
        List<String> authList = userMapper.getPermsByUserId(umsSysUser.getId());
        //TODO:根据用户查询权限信息  添加到LoginUser中
        return new LoginUser(umsSysUser,authList);
    }
}
